package uk.ac.lkl.takteen.teenEnergy.data;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.text.format.DateFormat;
import android.util.Log;

public class TeenEnergyDatabase extends SQLiteOpenHelper {

	private static final String DEBUG_TAG = "TeenEnergyDatabase";
	private static final String DB_NAME = "TeenEnergyData";
	private static final int DB_VERSION = 1;

	public TeenEnergyDatabase(Context context) {
		super(context, DB_NAME, null, DB_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// create user challenges table
		db.execSQL("create table " + TeenEnergyDatabaseMetadata.USER_CHALLENGES
				+ " (" + TeenEnergyDatabaseMetadata.ID
				+ " integer primary key autoincrement, "
				+ TeenEnergyDatabaseMetadata.CREATED_BY + " TEXT NOT NULL, "
				+ TeenEnergyDatabaseMetadata.LAST_MODIFIED_BY + " TEXT, "
				+ TeenEnergyDatabaseMetadata.CHALLENGE_NAME
				+ " TEXT NOT NULL, " + TeenEnergyDatabaseMetadata.DATE_CREATED
				+ " TEXT NOT NULL, "
				+ TeenEnergyDatabaseMetadata.CHALLENGE_IMAGE + " TEXT, "
				+ TeenEnergyDatabaseMetadata.CHALLENGE_TEXT + " TEXT, " + ");");
		// create received challenges table
		db.execSQL("create table "
				+ TeenEnergyDatabaseMetadata.RECEIVED_CHALLENGES + " ("
				+ TeenEnergyDatabaseMetadata.ID
				+ " integer primary key autoincrement, "
				+ TeenEnergyDatabaseMetadata.CREATED_BY + " TEXT NOT NULL, "
				+ TeenEnergyDatabaseMetadata.CHALLENGE_NAME
				+ " TEXT NOT NULL, " + TeenEnergyDatabaseMetadata.SENT_BY
				+ " TEXT NOT NULL, " + ");");
		// create user pledges table
		db.execSQL("create table " + TeenEnergyDatabaseMetadata.USER_PLEDGES
				+ " (" + TeenEnergyDatabaseMetadata.ID
				+ " integer primary key autoincrement, "
				+ TeenEnergyDatabaseMetadata.CREATED_BY + " TEXT NOT NULL "
				+ TeenEnergyDatabaseMetadata.PLEDGE_NAME + " TEXT NOT NULL, "
				+ TeenEnergyDatabaseMetadata.DATE_CREATED + " TEXT NOT NULL, "
				+ TeenEnergyDatabaseMetadata.EXPIRATION_DATE
				+ " TEXT NOT NULL, " + ");");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.w(DEBUG_TAG,
				"Upgrading database. Existing contents will be lost. ["
						+ oldVersion + "]->[" + newVersion + "]");
		db.execSQL("DROP TABLE IF EXISTS "
				+ TeenEnergyDatabaseMetadata.USER_CHALLENGES);
		db.execSQL("DROP TABLE IF EXISTS "
				+ TeenEnergyDatabaseMetadata.RECEIVED_CHALLENGES);
		db.execSQL("DROP TABLE IF EXISTS "
				+ TeenEnergyDatabaseMetadata.USER_PLEDGES);

		onCreate(db);
	}

}
